(function () {
    'use strict';

    angular
        .module('logcenterApp')
        .controller('partitionDashController', partitionDashController);
    partitionDashController.$inject = ['$scope', 'searchService', '$sce', 'ACTIVE', 'SvcAnalysisService', 'partitionDashService'];

    function partitionDashController($scope, searchService, $sce, active, SvcAnalysisService, partitionDashService) {
        var currentTime = new Date();
        var callServiceEchart = echarts.init(angular.element('#callServiceEchart')[0]);
        var commSendEchart = echarts.init(angular.element('#commSendEchart')[0]);
        var teleSendEchart = echarts.init(angular.element('#teleSendEchart')[0]);
        var callBatchEchart = echarts.init(angular.element('#callBatchEchart')[0]);
        var commReceiveEchart = echarts.init(angular.element('#commReceiveEchart')[0]);
        var teleReceiveEchart = echarts.init(angular.element('#teleReceiveEchart')[0]);




        $scope.show = [];
        $scope.code=[];
        $scope.partitionName = '';
        $scope.timeType = '1d';
        $scope.startTime = getToday00Clock().Format("yyyy-MM-ddThh:mm:ss.S");
        $scope.endTime = getToday11Clock().Format("yyyy-MM-ddThh:mm:ss.S");
        $scope.interval = 1800000;

        searchService.getPartition({}).then(
            function (resp) {
                $scope.partitionSource = toTree(resp.data);
                $scope.partition = resp.data[0].fname + '-' + resp.data[0].cname;
                $scope.partitionName = resp.data[0].ename;
                $scope.code= resp.data[0].code;
                angular.forEach(resp.data, function (item) {
                    $scope.show.push(item.ename);
                });

                queryCallservice();
                queryCommSend();
                queryTeleSend();
                queryCallBatch();
                queryCommReceive();
                queryTeleReceive();
            }, function (err) {
            }
        );

        $scope.selectShow = function (k) {
            angular.forEach($scope.show, function (item) {
                if (item == k.ename) {
                    $scope.partition = k.fname + "-" + k.cname;
                    $scope.partitionName = k.ename;
                }
            });
            $scope.code= k.code;
            queryCallservice();
            queryCommSend();
            queryTeleSend();
            queryCallBatch();
            queryCommReceive();
            queryTeleReceive();
            $scope.win.close();

        }


        //服务调用
        function queryCallservice() {
            partitionDashService.queryCallservice($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {
                    // var callServiceData = resp;
                    callServiceAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function callServiceAnalysis(callServiceData, partition) {
            var callServiceTime = [];
            var callServiceCount = [];

            if (callServiceData.length != 0) {
                angular.forEach(callServiceData, function (item) {
                        callServiceTime.push(timestampToTime(item.time));
                        callServiceCount.push(item.count);
                    }
                );
                var callServiceOption = {
                    color: ['#6FCA8E'],
                    title: {
                        text: partition + "服务调用走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%",

                        }
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '10%',
                        containLabel: true
                    },
                    legend: {
                        left:"right",
                        data: ['Count']
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: callServiceTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameLocation: "center",
                            nameGap: "30",
                            nameTextStyle: {},
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: "50",
                            splitLine: {
                                show: true
                            },
                            textStyle: {
                                fontSize: 18,
                                fontWeight: "bolder",
                            }
                        }
                    ],
                    series: [
                        {

                            name: "Count",
                            type: 'line',
                            barWidth: '60%',
                            data: callServiceCount
                        }
                    ]

                }
                callServiceEchart.setOption(callServiceOption, true);
            } else {
                var callServiceOption = {
                    title: {
                        text: partition + "服务调用走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }
                };
                callServiceEchart.setOption(callServiceOption, true);
            }
            ;

        }

        //异步路由发送
        function queryCommSend() {
            partitionDashService.queryCommSend($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {

                    // commSendData = resp;

                    commSendAnalysis(resp, $scope.partitionName);
                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function commSendAnalysis(commSendData, partition) {
            var xTime = [];
            var yCount = [];

            if (commSendData.length != 0) {
                angular.forEach(commSendData, function (item) {
                        xTime.push(timestampToTime(item.time));
                        yCount.push(item.count);
                    }
                );

                var commSendOption = {

                    color: ['#006E8A'],
                    title: {
                        text: partition + "异步路由发送走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left:"right",
                        data: ['异步路由发送次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameGap: "30",
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: "30",
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '异步路由发送次数',
                            type: 'line',
                            barWidth: '60%',
                            data: yCount
                        }
                    ]

                }
                commSendEchart.setOption(commSendOption, true);
            } else {
                var commSendOption = {
                    title: {
                        text: partition + "异步路由发送走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }
                };
                commSendEchart.setOption(commSendOption, true);
            }
            ;


        }


        //电文发送
        function queryTeleSend() {
            partitionDashService.queryTeleSend($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {

                    // var  teleSendData = resp;
                    teleSendAnalysis(resp, $scope.partitionName);

                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function teleSendAnalysis(teleSendData, partition) {
            var xTime = [];
            var yCount = [];
            if (teleSendData.length != 0) {
                angular.forEach(teleSendData, function (item) {
                        xTime.push(timestampToTime(item.time));
                        yCount.push(item.count);
                    }
                );
                var teleSendOption = {

                    color: ['#ab2100'],
                    title: {
                        text: partition + "电文发送走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left:"right",
                        data: ['4K电文发送次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameLocation: "center",
                            nameGap: "30",
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',

                            nameRotate: "90",
                            nameLocation: "center",

                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '4K电文发送次数',
                            type: 'line',
                            barWidth: '60%',
                            data: yCount
                        }
                    ]

                }
                teleSendEchart.setOption(teleSendOption, true);
            } else {
                var teleSendOption = {
                    title: {
                        text: partition + "电文发送走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }
                };
                teleSendEchart.setOption(teleSendOption, true);
            }
            ;

        }


        //批处理调用
        function queryCallBatch() {
            partitionDashService.queryCallBatch($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {
                    //var callBatchData = resp;
                    callBatchAnalysis(resp, $scope.partitionName);

                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function callBatchAnalysis(callBatchData, partition) {
            var xTime = [];
            var yCount = [];
            if (callBatchData.length != 0) {
                angular.forEach(callBatchData, function (item) {
                        xTime.push(timestampToTime(item.time));
                        yCount.push(item.count);
                    }
                );

                var callBatchOption = {

                    color: ['#414195'],
                    title: {
                        text: partition + "批处理调用走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left:"right",
                        data: ['批处理执行次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameLocation: "center",
                            nameGap: "30",
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '批处理执行次数',
                            type: 'line',
                            barWidth: '60%',
                            data: yCount
                        }
                    ]

                };
                callBatchEchart.setOption(callBatchOption, true);
            } else {
                var callBatchOption = {
                    title: {
                        text: partition + "批处理调用走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }

                };
                callBatchEchart.setOption(callBatchOption, true);
            }
            ;

        }


        //异步路由接收
        function queryCommReceive() {
            partitionDashService.querycommReceive($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {
                    //  var  commReceiveData = resp;

                    commReceiveAnalysis(resp, $scope.partitionName);

                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function commReceiveAnalysis(commReceiveData, partition) {
            var xTime = [];
            var yCount = [];
            if (commReceiveData.length != 0) {
                angular.forEach(commReceiveData, function (item) {
                        xTime.push(timestampToTime(item.time));
                        yCount.push(item.count);
                    }
                );

                var commReceiveOption = {

                    color: ['#BC52BC'],
                    title: {
                        text: partition + "异步路由接收走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left:"right",
                        data: ['异步路由接收次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameLocation: "center",
                            nameGap: "30",
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '异步路由接收次数',
                            type: 'line',
                            barWidth: '60%',
                            data: yCount
                        }
                    ]

                };
                commReceiveEchart.setOption(commReceiveOption, true);
            } else {
                var commReceiveOption = {
                    title: {
                        text: partition + "异步路由接收走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }
                }
                commReceiveEchart.setOption(commReceiveOption, true);
            }
            ;

        }


        //电文接收
        function queryTeleReceive() {
            partitionDashService.queryteleReceive($scope.partitionName, $scope.startTime, $scope.endTime, $scope.interval,$scope.code).then(
                function (resp) {
                    teleReceiveAnalysis(resp, $scope.partitionName);

                }, function (error) {
                    console.log(error);
                    notify('error', '失败', error.message);
                });
        }

        function teleReceiveAnalysis(teleReceiveData, partition) {
            var xTime = [];
            var yCount = [];
            if (teleReceiveData.length != 0) {
                angular.forEach(teleReceiveData, function (item) {
                        xTime.push(timestampToTime(item.time));
                        yCount.push(item.count);
                    }
                );

                var teleReceiveOption = {

                    color: ['#6F87D8'],
                    title: {
                        text: partition + "电文接收走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 14,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    },
                    legend: {
                        left:"right",
                        data: ['4K电文接受次数']
                    },
                    tooltip: {
                        trigger: 'axis',
                        axisPointer: {            // 坐标轴指示器，坐标轴触发有效
                            type: 'line'        // 默认为直线，可选为：'line' | 'shadow'
                        },
                        formatter: function (params) {
                            var res = "";
                            angular.forEach(params, function (item) {
                                res += "<span >Count:" + "&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;" + item.value + '</span>' + '<br>' + '<hr>'
                                    + "<span>" + '@timestamp per' + changeMin($scope.interval) + ' minutes :' + "&emsp;" + item.name + "</span>"
                            });
                            return res;
                        }
                    },
                    grid: {
                        left: '4%',
                        right: '4%',
                        bottom: '8%',
                        containLabel: true
                    },
                    xAxis: [
                        {
                            type: 'category',
                            data: xTime,
                            name: '@timestamp per' + changeMin($scope.interval) + ' minutes',
                            nameLocation: "center",
                            nameGap: "30",
                            axisTick: {
                                alignWithLabel: true
                            }
                        }
                    ],
                    yAxis: [
                        {
                            type: 'value',
                            nameRotate: "90",
                            nameLocation: "center",
                            nameGap: 40,
                            splitLine: { //网格线
                                show: true
                            },
                            textStyle: {
                                fontSize: 14,
                                fontWeight: "bold",
                            }
                        }
                    ],
                    series: [
                        {
                            name: '4K电文接受次数',
                            type: 'line',
                            barWidth: '60%',
                            data: yCount
                        }
                    ]

                };
                teleReceiveEchart.setOption(teleReceiveOption, true);
            } else {
                var teleReceiveOption = {
                    title: {
                        text: partition + "批处理调用走势",
                        textStyle: {
                            color: "#6d6d6d",
                            fontSize: 15,
                            fontWeight: "bold",
                            top: "2%"
                        }
                    }

                }
                teleReceiveEchart.setOption(teleReceiveOption, true);
            };
        }

        $scope.select = function () {
            if ($scope.timeType == '1d') {
                $scope.startTime = getToday00Clock().Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.endTime = getToday11Clock().Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.interval = 1800000;
                queryCallservice();
                queryCommSend();
                queryTeleSend();
                queryCallBatch();
                queryCommReceive();
                queryTeleReceive();

            } else if ($scope.timeType == '1h') {
                currentTime.setHours(currentTime.getHours() - 1);
                $scope.startTime = (currentTime.Format("yyyy-MM-ddThh:mm:ss.S"));
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.interval = 60000;
                queryCallservice();
                queryCommSend();
                queryTeleSend();
                queryCallBatch();
                queryCommReceive();
                queryTeleReceive();

            } else if ($scope.timeType == '4h') {
                currentTime.setHours(currentTime.getHours() - 4);
                $scope.startTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.interval = 300000;
                queryCallservice();
                queryCommSend();
                queryTeleSend();
                queryCallBatch();
                queryCommReceive();
                queryTeleReceive();

            } else if ($scope.timeType == '12h') {
                currentTime.setHours(currentTime.getHours() - 12);
                $scope.startTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                currentTime = new Date();
                $scope.endTime = currentTime.Format("yyyy-MM-ddThh:mm:ss.S");
                $scope.interval = 600000;
                queryCallservice();
                queryCommSend();
                queryTeleSend();
                queryCallBatch();
                queryCommReceive();
                queryTeleReceive();

            }
        }
        //图表自适应
        window.onresize = function(){
            callServiceEchart.resize();
            commSendEchart.resize();
            teleSendEchart.resize();
            callBatchEchart.resize();
            commReceiveEchart.resize();
            teleReceiveEchart.resize();
        }
        function add0(m) {
            return m < 10 ? '0' + m : m
        }




    }

})();
